package cn.highedu.coolshark.product.controller;

import cn.highedu.coolshark.common.JsonResult;
import cn.highedu.coolshark.common.PagedVO;
import cn.highedu.coolshark.product.pojo.dto.BrandAddNewDTO;
import cn.highedu.coolshark.product.pojo.dto.BrandQueryParamDTO;
import cn.highedu.coolshark.product.pojo.dto.BrandUpdateDTO;
import cn.highedu.coolshark.product.pojo.vo.BrandListItemVO;
import cn.highedu.coolshark.product.pojo.vo.BrandStandardVO;
import cn.highedu.coolshark.product.service.IBrandService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理品牌相关请求的控制器
 */
@RestController
@RequestMapping("/brands")
@Api(tags = "品牌管理模块")
@Validated
@Slf4j
public class BrandController {
    @Autowired
    private IBrandService brandService;

    @GetMapping("")
    @ApiOperation("查询品牌列表")
    @ApiResponse(code = 20000, message = "OK", response = BrandListItemVO.class)
    public JsonResult<List<BrandListItemVO>> list() {
        log.debug("开始处理【查询品牌列表】的请求, 无参数");
        List<BrandListItemVO> list = brandService.list();
        return JsonResult.ok(list);
    }
    @PostMapping("/paged")
    @ApiOperation("分页查询品牌列表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "brandQueryParamDTO", value = "品牌分页查询参数", required = true,
                dataType = "BrandQueryParamDTO", paramType = "body")
    })
    @ApiResponse(code = 20000, message = "OK", response = PagedVO.class)
    // @PreAuthorize("hasAuthority('/brand/list')")
    public JsonResult<PagedVO<BrandListItemVO>> pagedList(@RequestBody BrandQueryParamDTO brandQueryParamDTO) {
        log.debug("开始处理【分页查询品牌列表】的请求, 参数: {}", brandQueryParamDTO);
        PagedVO<BrandListItemVO> pagedVO = brandService.list(brandQueryParamDTO);
        return JsonResult.ok(pagedVO);
    }

    /**
     * 启用品牌
     * @return
     */
    @PostMapping("/{id}/enable")
    @ApiOperation("启用品牌")
    @ApiImplicitParams(value = {
        @ApiImplicitParam(name = "id", value = "品牌ID", required = true, dataType = "long")
    })
    @ApiResponse(code = 20000, message = "OK", response = JsonResult.class)
    public JsonResult<Void> setEnable(@PathVariable Long id) {
        log.debug("开始处理【启用品牌】的请求, 参数: {}", id);
        brandService.setEnable(id);
        return JsonResult.ok();
    }
    /**
     * 禁用品牌
     * @return
     */
    @PostMapping("/{id}/disable")
    @ApiOperation("禁用品牌")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "品牌ID", required = true, dataType = "long")
    })
    @ApiResponse(code = 20000, message = "OK", response = JsonResult.class)
    public JsonResult<Void> setDisable(@PathVariable Long id) {
        log.debug("开始处理【禁用品牌】的请求, 参数: {}", id);
        brandService.setDisable(id);
        return JsonResult.ok();
    }
    /**
     * 删除品牌
     */
    @PostMapping("/{id}/delete")
    @ApiOperation("根据ID删除品牌")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "品牌ID", required = true, dataType = "long")
    })
    @ApiResponse(code = 20000, message = "OK", response = JsonResult.class)
    public JsonResult<Void> delete(@PathVariable Long id) {
        log.debug("开始处理【根据ID删除品牌】的请求, 参数: {}", id);
        brandService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/add-new")
    @ApiOperation("添加品牌")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "brandAddNewDTO", value = "品牌新增数据传输对象", required = true,
                    dataType = "BrandAddNewDTO", paramType = "body")
    })
    @ApiResponse(code = 20000, message = "OK", response = JsonResult.class)
    public JsonResult<Void> addNew(@RequestBody @Valid BrandAddNewDTO brandAddNewDTO) {
        log.debug("开始处理【添加品怕】的请求, 参数: {}", brandAddNewDTO);
        brandService.addNew(brandAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id}/update")
    @ApiOperation("修改品牌详情")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "品牌ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "brandUpdateDTO", value = "品牌修改数据传输对象", required = true,
            dataType = "BrandUpdateDTO", paramType = "body")
    })
    @ApiResponse(code = 20000, message = "OK", response = JsonResult.class)
    public JsonResult<Void> updateById(@PathVariable Long id, @RequestBody BrandUpdateDTO brandUpdateDTO) {
        log.debug("开始处理【修改品牌详情】的请求, 参数ID: {}, 数据: {}", id, brandUpdateDTO);
        brandService.updateInfoById(id, brandUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询品牌详情")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "品牌ID", required = true, dataType = "long")
    })
    @ApiResponse(code = 20000, message = "OK", response = JsonResult.class)
    public JsonResult<BrandStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据ID查询品牌详情】的请求, 参数: {}", id);
        BrandStandardVO brand = brandService.getStandardById(id);
        return JsonResult.ok(brand);
    }
}
